package org.javaqq.face.main.base;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;

public class IconCellRenderer 
extends    JPanel
implements TreeCellRenderer
{
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;

protected boolean m_selected;

private final JLabel statusLabel = new JLabel();

private final JLabel nameLabel = new JLabel();

private final GridBagConstraints constraints = new GridBagConstraints();

public IconCellRenderer()
{
  super(new GridBagLayout());
  m_textSelectionColor = UIManager.getColor(
    "Tree.selectionForeground");
  m_textNonSelectionColor = UIManager.getColor(
    "Tree.textForeground");
  m_bkSelectionColor = UIManager.getColor(
    "Tree.selectionBackground");
  m_bkNonSelectionColor = UIManager.getColor(
    "Tree.textBackground");
  m_borderSelectionColor = UIManager.getColor(
    "Tree.selectionBorderColor");
  this.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 2));
  this.setOpaque(false);
  this.nameLabel.setOpaque(false);
  statusLabel.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 2));

  constraints.anchor = GridBagConstraints.WEST;
  constraints.fill = GridBagConstraints.NONE;
  constraints.gridx = 0;
  constraints.gridy = 0;
  constraints.gridheight = 1;
  constraints.weightx = 0f;
  constraints.weighty = 1f;
  this.add(statusLabel, constraints);

  constraints.anchor = GridBagConstraints.WEST;
  constraints.fill = GridBagConstraints.VERTICAL;
  constraints.gridx = 1;
  constraints.gridy = 0;
  constraints.weightx = 1f;
  constraints.weighty = 0f;
  constraints.gridheight = 1;
  constraints.gridwidth = 2;
  this.add(nameLabel, constraints);
}

public Component getTreeCellRendererComponent(JTree tree, 
  Object value, boolean sel, boolean expanded, boolean leaf, 
  int row, boolean hasFocus) 
  
{
  DefaultMutableTreeNode node = 
    (DefaultMutableTreeNode)value;
  Object obj = node.getUserObject();
  nameLabel.setText(obj.toString());
  if (obj instanceof IconData)
  {
    IconData idata = (IconData)obj;
    if (expanded)
  	  statusLabel.setIcon(idata.getOpenIcon());
    else
  	  statusLabel.setIcon(idata.getIcon());
  }
  else
  	statusLabel.setIcon(null);

  setFont(tree.getFont());
  setForeground(sel ? m_textSelectionColor : 
    m_textNonSelectionColor);
  setBackground(sel ? m_bkSelectionColor : 
    m_bkNonSelectionColor);
  m_selected = sel;
  return this;
}
  
public void paint(Graphics g) 
{
  Color bColor = getBackground();
  Icon icon = statusLabel.getIcon();

  g.setColor(bColor);
  int offset = 0;
  if(icon != null && nameLabel.getText() != null) 
    offset = (icon.getIconWidth() + nameLabel.getIconTextGap());
  g.fillRect(offset, 0, getWidth() - 1 - offset,
    getHeight() - 1);
  
  if (m_selected) 
  {
    g.setColor(m_borderSelectionColor);
    g.drawRect(offset, 0, getWidth()-1-offset, getHeight()-1);
  }

  super.paint(g);
  }
}